package com.ruoyi.framework.datasource;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.SysSourceBdsConfig;
import com.ruoyi.system.domain.vo.AreaSourceVo;
import com.ruoyi.system.service.ISysSourceBdsConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jierui
 * @package com.ruoyi.framework.datasource
 * @date 2022/4/22-9:29
 * @description:
 */
@Component
public class AreaSourceConfig {

    @Resource
    private RedisCache redisCache;

    @Autowired
    private ISysSourceBdsConfigService iSysSourceBdsConfigService;

    private static final String AREA_SOURCE_CODE="AREA_SOURCE_CODE";

    @PostConstruct
    public void initAreaSource(){
        List<SysSourceBdsConfig> sysSourceBdsConfigs = iSysSourceBdsConfigService.selectSysSourceBdsConfigList(new SysSourceBdsConfig());
        redisCache.deleteObject(AREA_SOURCE_CODE);
        if(CollectionUtils.isNotEmpty(sysSourceBdsConfigs)){
           for(SysSourceBdsConfig sysSourceBdsConfig : sysSourceBdsConfigs){
               String bdsId=sysSourceBdsConfig.getBdsId();
               String sourceCode=sysSourceBdsConfig.getSourceCode();
               redisCache.setCacheMapValueTime(AREA_SOURCE_CODE,bdsId,new AreaSourceVo(bdsId,sourceCode),-1L);
           }
        }

    }

    /**
     * 获取电站数据源配置
     * @return bdsId,sourceCode
     */
    public Map<String, AreaSourceVo> getAreaSourceMap(){
        return redisCache.getCacheMap(AREA_SOURCE_CODE);
    }

    public AreaSourceVo getAreaSource(String bdsId){
        return redisCache.getCacheMapValue(AREA_SOURCE_CODE,bdsId);
    }


}
